import QtQuick 2.0
import QtQuick.Controls 2.2

import "../../common"
import "../../../js/util.js" as Util
import "../../../js/UI.js" as Style

Page {
    id: loginpage
//    width: parent.width
//    height: parent.height

    Component.onCompleted: {
        m_user_text.text = DatabaseEngine.local.id
        m_pwd_text.text = DatabaseEngine.local.pwd
    }

    Image{
        anchors.fill: parent
        source: "qrc:/image/login/login.jfif"
    }
    Item{
        id: login_rec
        width: parent.width
        height: m_lineValue*5
        anchors.top: parent.top
        anchors.topMargin: m_lineValue*6
        Item{
            id: m_user_id
            width: parent.width*0.8
            height: m_lineValue
            anchors.top: parent.top
            anchors.horizontalCenter: parent.horizontalCenter
            Image {
                id: m_user_image
                anchors.left: parent.left
                anchors.leftMargin: 0.3*m_lineValue
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height*0.6
                width: height
                source: "qrc:/image/login/userid.png"
            }
            TextInput{
                id: m_user_text
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: m_user_image.right
                anchors.leftMargin: 0.2*m_lineValue
                anchors.right: parent.right
                font.pointSize: 16
            }

            Rectangle{
                width: parent.width
                height: 1
                color: "#8a8c8e"
                anchors.bottom: parent.bottom
            }
        }
        Item{
            id: m_pwd_id
            width: parent.width*0.8
            height: m_lineValue
            anchors.top: m_user_id.bottom
            anchors.topMargin: m_lineValue*0.5
            anchors.horizontalCenter: parent.horizontalCenter
            Image {
                id: m_pwd_image
                anchors.left: parent.left
                anchors.leftMargin: 0.3*m_lineValue
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height*0.6
                width: height
                source: "qrc:/image/login/password.png"
            }
            TextInput{
                id: m_pwd_text
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: m_pwd_image.right
                anchors.leftMargin: 0.2*m_lineValue
                anchors.right: parent.right
                font.pointSize: 14
                echoMode: TextInput.Password
            }
            Rectangle{
                width: parent.width
                height: 1
                color: "#8a8c8e"
                anchors.bottom: parent.bottom
            }
        }
        Item{
            id: m_remen
            width: parent.width*0.8
            height: m_lineValue
            anchors.top: m_pwd_id.bottom
            anchors.topMargin: m_lineValue*0.2
            anchors.horizontalCenter: parent.horizontalCenter
            CheckBox {
                id: checkBox
                text: qsTr("记住密码")
                checked: true
                spacing: 0
                enabled: true
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 0.3*m_lineValue
                indicator:Image {
                    id: image
                    height: m_remen.height*0.5
                    width: m_remen.height*0.5
                    anchors.verticalCenter: parent.verticalCenter
                    source: checkBox.checked ? "qrc:/image/login/checked.png" : "qrc:/image/login/unchecked.png"
                }

                contentItem: Text {
                    id: text
                    anchors.left: image.right
                    text: checkBox.text
                    font: checkBox.font
                    opacity: enabled ? 1.0 : 0.3
                    color: checkBox.checked ? "#1296db" : "#000000"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    //                        leftPadding: checkBox.indicator.width + checkBox.spacing
                }
            }
        }
        Item{
            id: m_login_but
            width: parent.width*0.8
            height: m_lineValue
            anchors.top: m_remen.bottom
            anchors.topMargin: 0.6*m_lineValue
            anchors.horizontalCenter: parent.horizontalCenter
            PushButton{
                width: parent.width
                height: parent.height
                anchors.centerIn: parent
                Accessible.focusable : true
                radius: 30
                m_text: "登录"
                MouseArea{
                    id: mouseid
                    anchors.fill: parent
                    onClicked: {
                        console.log("正在登录中........")
                        if(checkBox.checked)
                            DatabaseEngine.rememberlocal(m_user_text.text,m_pwd_text.text)
                        else
                            DatabaseEngine.rememberlocal(m_user_text.text,"");
                        DatabaseEngine.log(m_user_text.text,m_pwd_text.text)
                    }
                }
                activeFocusOnTab :true
                color: mouseid.pressed?Style.menuColor:Style.TitleColor
            }
        }
    }
    Connections{
        target: DatabaseEngine
        onPasswordPairChanged:{
            if(!DatabaseEngine.passwordpair){
                var str = "账号或密码错误"
                var temp = Util.showPopWin(m_root_id,"qrc:/qml/form/dialog/Dialog_sure_and_cancel_warning.qml",str,function (retValue){});
                temp.sure_cancel_signal.disconnect(onSure_cancel_signal)
                temp.sure_cancel_signal.connect(onSure_cancel_signal)
            }
        }
    }
    function onSure_cancel_signal(value)
    {
        if(value)
            m_pwd_text.text = "";
    }
}
